﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

    [Serializable]
    public class ImgNewsData
    {

        public const string IDField = "@ID";

        public const string PRIMARYKEY_Field = @"ID";

        public const string IMGField = "@Img";

        public const string TITLEField = "@Title";

        public const string TXTField = "@Txt";

        public const string CATEGORYField = "@Category";

        public const string TOPField = "@Top";

        public const string MEMBERNAMEField = "@MemberName";

        public const string EDITDATEField = "@EditDate";

        public const string T1Field = "@t1";

        public const string T2Field = "@t2";

        public const string T3Field = "@t3";

        private Guid _ID;

        private String _Img;

        private String _Title;

        private String _Txt;

        private String _Category;

        private Boolean _Top;

        private String _MemberName;

        private DateTime _EditDate;

        private String _t1;

        private String _t2;

        private String _t3;

        public Guid ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public String Img
        {
            get { return _Img; }
            set { _Img = value; }
        }
        public String Title
        {
            get { return _Title; }
            set { _Title = value; }
        }
        public String Txt
        {
            get { return _Txt; }
            set { _Txt = value; }
        }
        public String Category
        {
            get { return _Category; }
            set { _Category = value; }
        }
        public Boolean Top
        {
            get { return _Top; }
            set { _Top = value; }
        }
        public String MemberName
        {
            get { return _MemberName; }
            set { _MemberName = value; }
        }
        public DateTime EditDate
        {
            get { return _EditDate; }
            set { _EditDate = value; }
        }
        public String t1
        {
            get { return _t1; }
            set { _t1 = value; }
        }
        public String t2
        {
            get { return _t2; }
            set { _t2 = value; }
        }
        public String t3
        {
            get { return _t3; }
            set { _t3 = value; }
        }
        public ImgNewsData()
        {
            _ID = Guid.Empty;
            _Img = "";
            _Title = "";
            _Txt = "";
            _Category = "";
            _Top = false;
            _MemberName = "";
            _EditDate = DateTime.Now;
            _t1 = "";
            _t2 = "";
            _t3 = "";
        }
    }

    public class ImgNewsDataFactory
    {
        public static ImgNewsData Construct(IDataReader reader)
        {
            ImgNewsData data = new ImgNewsData();

            try { data.ID = (Guid)(reader["ID"]); }
            catch { }

            try { data.Img = Convert.ToString(reader["Img"]); }
            catch { }

            try { data.Title = Convert.ToString(reader["Title"]); }
            catch { }

            try { data.Txt = Convert.ToString(reader["Txt"]); }
            catch { }

            try { data.Category = Convert.ToString(reader["Category"]); }
            catch { }

            try { data.Top = Convert.ToBoolean(reader["Top"]); }
            catch { }

            try { data.MemberName = Convert.ToString(reader["MemberName"]); }
            catch { }

            try { data.EditDate = Convert.ToDateTime(reader["EditDate"]); }
            catch { }

            try { data.t1 = Convert.ToString(reader["t1"]); }
            catch { }

            try { data.t2 = Convert.ToString(reader["t2"]); }
            catch { }

            try { data.t3 = Convert.ToString(reader["t3"]); }
            catch { }

            return data;
        }
        public static Guid GetPrimaryKey(DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static Guid GetPrimaryKey(DataTable dt, int rowIndex)
        {
            Guid PrimaryKey = Guid.Empty;

            if (dt.Rows.Count < rowIndex + 1)

                return Guid.Empty;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return Guid.Empty;

            try { PrimaryKey = (Guid)(dr[ImgNewsData.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static ImgNewsData Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static ImgNewsData Construct(DataTable dt, int rowIndex)
        {
            ImgNewsData data = new ImgNewsData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.ID = new Guid(dr["ID"].ToString()); }
            catch { }

            try { data.Img = Convert.ToString(dr["Img"]); }
            catch { }

            try { data.Title = Convert.ToString(dr["Title"]); }
            catch { }

            try { data.Txt = Convert.ToString(dr["Txt"]); }
            catch { }

            try { data.Category = Convert.ToString(dr["Category"].ToString()); }
            catch { }

            try { data.Top = Convert.ToBoolean(dr["Top"]); }
            catch { }

            try { data.MemberName = Convert.ToString(dr["MemberName"]); }
            catch { }

            try { data.EditDate = Convert.ToDateTime(dr["EditDate"]); }
            catch { }

            try { data.t1 = Convert.ToString(dr["t1"]); }
            catch { }

            try { data.t2 = Convert.ToString(dr["t2"]); }
            catch { }

            try { data.t3 = Convert.ToString(dr["t3"]); }
            catch { }

            return data;
        }        
        public static ImgNewsData Construct(DataRow dr)
        {
            ImgNewsData data = new ImgNewsData();

            try { data.ID = new Guid(dr["ID"].ToString()); }
            catch { }

            try { data.Img = Convert.ToString(dr["Img"]); }
            catch { }

            try { data.Title = Convert.ToString(dr["Title"]); }
            catch { }

            try { data.Txt = Convert.ToString(dr["Txt"]); }
            catch { }

            try { data.Category = Convert.ToString(dr["Category"].ToString()); }
            catch { }

            try { data.Top = Convert.ToBoolean(dr["Top"]); }
            catch { }

            try { data.MemberName = Convert.ToString(dr["MemberName"]); }
            catch { }

            try { data.EditDate = Convert.ToDateTime(dr["EditDate"]); }
            catch { }

            try { data.t1 = Convert.ToString(dr["t1"]); }
            catch { }

            try { data.t2 = Convert.ToString(dr["t2"]); }
            catch { }

            try { data.t3 = Convert.ToString(dr["t3"]); }
            catch { }

            return data;
        }
        public static Guid GetPrimaryKey(DataRow dr)
        {
            Guid PrimaryKey = Guid.Empty;

            try { PrimaryKey = (Guid)(dr[ImgNewsData.PRIMARYKEY_Field]); }
            catch { }
            return PrimaryKey;
        }

    }
    public class ImgNewsDataDBOption
    {
        public static int Insert(ImgNewsData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into ImgNews (");
            sql.Append("Img, ");
            sql.Append("Title, ");
            sql.Append("Txt, ");
            sql.Append("Category, ");
            sql.Append("[Top], ");
            sql.Append("MemberName, ");
            sql.Append("EditDate, ");
            sql.Append("t1, ");
            sql.Append("t2, ");
            sql.Append("t3");
            sql.Append(") values (");
            sql.Append("'");
            sql.Append(d.Img.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Title.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Txt.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Category.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(Convert.ToByte(d.Top));
            sql.Append("");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.MemberName.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.EditDate.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.t1.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.t2.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.t3.ToString());
            sql.Append("'");
            sql.Append(")");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static int Update(ImgNewsData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update ImgNews set ");
            sql.Append("Img=");
            sql.Append("'");
            sql.Append(d.Img.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Title=");
            sql.Append("'");
            sql.Append(d.Title.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Txt=");
            sql.Append("'");
            sql.Append(d.Txt.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Category=");
            sql.Append("'");
            sql.Append(d.Category.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("[Top]=");
            sql.Append("");
            sql.Append(Convert.ToByte(d.Top));
            sql.Append("");
            sql.Append(", ");
            sql.Append("MemberName=");
            sql.Append("'");
            sql.Append(d.MemberName.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("EditDate=");
            sql.Append("'");
            sql.Append(d.EditDate.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("t1=");
            sql.Append("'");
            sql.Append(d.t1.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("t2=");
            sql.Append("'");
            sql.Append(d.t2.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("t3=");
            sql.Append("'");
            sql.Append(d.t3.ToString());
            sql.Append("'");
            sql.Append(" where ID='");
            sql.Append(d.ID.ToString());
            sql.Append("'");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static int Delete(ImgNewsData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from ImgNews ");
            sql.Append(" where ID='");
            sql.Append(d.ID.ToString()+"'");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static DataTable DataTableSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from ImgNews ");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataTable DataTableSelect(ImgNewsData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from ImgNews where ID='");
            sql.Append(d.ID.ToString()+"'");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataSet DataSetSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from ImgNews ");
            return WFNetLib.ADO.SQLServerOP.DataSetSQL(sql.ToString());
        }
        public static SqlDataReader ReaderSelect(ImgNewsData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from ImgNews ");
            return WFNetLib.ADO.SQLServerOP.ReaderExecuteSQL(sql.ToString());
        }
        public static void SetTop(ImgNewsData d)
        {
            string sql;
            if(d.Top)
                sql= "UPDATE ImgNews SET [Top] =0 WHERE (ID = '"+d.ID+"')";
            else
                sql = "UPDATE ImgNews SET [Top] =1 WHERE (ID = '" + d.ID + "')";
            WFNetLib.ADO.SQLServerOP.ReaderExecuteSQL(sql);
        }
    }